﻿/*  ----------------------------------------------------------------------------
 *  EuphoricNation
 *  ----------------------------------------------------------------------------
 *  EuphoricEngine v1.0
 *  ----------------------------------------------------------------------------
 *  File:       Sprite.cs
 *  Author:     Shane-PC\Shane
 *  ----------------------------------------------------------------------------
 */

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace EuphoricEngine.Components.Components2D
{
    public class Sprite : Component
    {
        private bool _originOverrided = false;

        private Vector2 _position;
        private Color _color;
        private float _rotation;
        private float _layer = 0f;
        private float _scale = 1f;
        private SpriteEffects _effects;
        private Vector2 _origin = Vector2.Zero;

        private Texture2D _texture;
        private string _textureName;

        public Sprite(Vector2 position)
        {
            _layer = 0f;
            _rotation = 0f;
            _color = Color.White;
            _effects = SpriteEffects.None;

            _position = position;
        }
        public Sprite(string texturePath, Vector2 position)
        {
            _layer = 0f;
            _rotation = 0f;
            _color = Color.White;
            _effects = SpriteEffects.None;

            _position = position;
            _textureName = texturePath;
        }
        public Sprite(Texture2D texture, Vector2 position)
        {
            _layer = 0f;
            _rotation = 0f;
            _color = Color.White;
            _effects = SpriteEffects.None;

            _position = position;
            _texture = texture;
        }

        protected override void Load()
        {
            if (!string.IsNullOrEmpty(_textureName))
            {
                _texture = Parent.Engine.Content.Load<Texture2D>(_textureName);
            }
        }

        public override void Update()
        {
            if (!_originOverrided && _texture != null)
            {
                _origin = new Vector2(
                    (float)_texture.Width * 0.5f,
                    (float)_texture.Height * 0.5f
                );
            }

            base.Update();
            _originOverrided = false;
        }

        public override void Draw()
        {
            base.Draw();

            if (_texture != null)
            {
                Parent.Engine.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
                Parent.Engine.SpriteBatch.Draw(
                    _texture,
                    _position,
                    new Rectangle(0, 0, _texture.Width, _texture.Height),
                    _color,
                    _rotation,
                    _origin,
                    _scale,
                    _effects,
                    _layer
                );
                Parent.Engine.SpriteBatch.End();
            }
        }

        public void SetColor(Color color)
        {
            _color = color;
        }

        public virtual void SetScale(float scale)
        {
            _scale = scale;
        }

        public virtual void SetRotation(float rotation)
        {
            _rotation = rotation;
        }

        public virtual void SetPosition(Vector2 position)
        {
            _position = position;
        }

        public virtual void SetPositionX(float X)
        {
            _position = new Vector2(X, _position.Y);
        }

        public virtual void SetPositionY(float Y)
        {
            _position = new Vector2(_position.X, Y);
        }

        public void SetLayer(float layer)
        {
            _layer = MathHelper.Clamp(layer, 0f, 1f);
        }

        public void SetSpriteEffects(SpriteEffects spriteEffects)
        {
            _effects = spriteEffects;
        }

        public void SetTexture(Texture2D texture)
        {
            _texture = texture;
        }

        public void OverrideOrigin(Vector2 origin)
        {
            _originOverrided = true;
            _origin = origin;
        }

        public Vector2 Position
        {
            get { return _position; }
        }

        public Color Color
        {
            get { return _color; }
        }

        public float Rotation
        {
            get { return _rotation; }
        }

        public Texture2D Texture
        {
            get { return _texture; }
        }

        public float Layer
        {
            get { return _layer; }
        }

        public SpriteEffects SpriteEffects
        {
            get { return _effects; }
        }

        public Vector2 Origin
        {
            get { return _origin; }
        }

        public float Scale
        {
            get { return _scale; }
        }
    }
}
